home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 118 / cd-rom 118.iso / aplic / open / openofficeorg1.cab / prefcalls.js < prev    next >
Encoding:
Text File  |  2004-02-14  |  6.9 KB  |  229 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Mitesh Shah <mitesh@netscape.com>
  24.  * Brian Nesse <bnesse@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. const nsILDAPURL = Components.interfaces.nsILDAPURL;
  41. const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
  42. const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
  43. const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
  44. const nsIPrefService = Components.interfaces.nsIPrefService;
  45. const PrefServiceContractID = "@mozilla.org/preferences-service;1";
  46.  
  47. // set on a platform specific basis in platform.js
  48. platform = { value: "" };
  49.  
  50. // default to LDAP v3
  51. var gVersion = Components.interfaces.nsILDAPConnection.VERSION3;
  52.  
  53. function getPrefBranch() {
  54.     
  55.     var prefService = Components.classes[PrefServiceContractID]
  56.                                 .getService(nsIPrefService);    
  57.     return prefService.getBranch(null);
  58. }
  59.  
  60. function pref(prefName, value) {
  61.  
  62.     try { 
  63.         var prefBranch = getPrefBranch();
  64.  
  65.         if (typeof value == "string") {
  66.             prefBranch.setCharPref(prefName, value);
  67.         }
  68.         else if (typeof value == "number") {
  69.             prefBranch.setIntPref(prefName, value);
  70.         }
  71.         else if (typeof value == "boolean") {
  72.             prefBranch.setBoolPref(prefName, value);
  73.         }
  74.     }
  75.     catch(e) {
  76.         displayError("pref", e);
  77.     }
  78. }
  79.  
  80. function defaultPref(prefName, value) {
  81.     
  82.     try {
  83.         var prefService = Components.classes[PrefServiceContractID]
  84.                                     .getService(nsIPrefService);        
  85.         var prefBranch = prefService.getDefaultBranch(null);
  86.         if (typeof value == "string") {
  87.             prefBranch.setCharPref(prefName, value);
  88.         }
  89.         else if (typeof value == "number") {
  90.             prefBranch.setIntPref(prefName, value);
  91.         }
  92.         else if (typeof value == "boolean") {
  93.             prefBranch.setBoolPref(prefName, value);
  94.         }
  95.     }
  96.     catch(e) {
  97.         displayError("defaultPref", e);
  98.     }
  99. }
  100.  
  101. function lockPref(prefName, value) {
  102.     
  103.     try {
  104.         var prefBranch = getPrefBranch();
  105.         
  106.         if (prefBranch.prefIsLocked(prefName))
  107.             prefBranch.unlockPref(prefName);
  108.         
  109.         defaultPref(prefName, value);
  110.         
  111.         prefBranch.lockPref(prefName);
  112.     }
  113.     catch(e) {
  114.         displayError("lockPref", e);
  115.     }
  116. }
  117.  
  118. function unlockPref(prefName) {
  119.  
  120.     try {
  121.  
  122.         var prefBranch = getPrefBranch();
  123.         prefBranch.unlockPref(prefName);
  124.     }
  125.     catch(e) {
  126.         displayError("unlockPref", e);
  127.     }
  128. }
  129.  
  130. function getPref(prefName) {
  131.     
  132.     try {
  133.         var prefBranch = getPrefBranch();
  134.         
  135.         switch (prefBranch.getPrefType(prefName)) {
  136.             
  137.         case prefBranch.PREF_STRING:
  138.             return prefBranch.getCharPref(prefName);
  139.             
  140.         case prefBranch.PREF_INT:
  141.             return prefBranch.getIntPref(prefName);
  142.             
  143.         case prefBranch.PREF_BOOL:
  144.             return prefBranch.getBoolPref(prefName);
  145.         default:
  146.             return null;
  147.         }
  148.     }
  149.     catch(e) {
  150.         displayError("getPref", e);
  151.     }
  152. }
  153.  
  154.  
  155. function setLDAPVersion(version) {
  156.     gVersion = version;
  157. }
  158.  
  159.  
  160. function getLDAPAttributes(host, base, filter, attribs) {
  161.     
  162.     try {
  163.         var url = Components.classes[LDAPURLContractID].createInstance(nsILDAPURL);
  164.     
  165.         url.spec = "ldap://" + host + "/" + base + "?" + attribs 
  166.                    + "?sub?" +  filter;
  167.         var ldapquery = Components.classes[LDAPSyncQueryContractID]
  168.                                   .createInstance(nsILDAPSyncQuery);
  169.     // user supplied method
  170.         processLDAPValues(ldapquery.getQueryResults(url, gVersion));
  171.     }
  172.     catch(e) {
  173.         displayError("getLDAPAttibutes", e);
  174.     }
  175. }
  176.  
  177. function getLDAPValue(str, key) {
  178.  
  179.     try {
  180.         if (str == null || key == null)
  181.             return null;
  182.         
  183.         var search_key = "\n" + key + "=";
  184.         
  185.         var start_pos = str.indexOf(search_key);
  186.         if (start_pos == -1)
  187.             return null;
  188.         
  189.         start_pos += search_key.length;
  190.         
  191.         var end_pos = str.indexOf("\n", start_pos);
  192.         if (end_pos == -1)
  193.             end_pos = str.length;
  194.         
  195.         return str.substring(start_pos, end_pos);
  196.     }
  197.     catch(e) {
  198.         displayError("getLDAPValue", e);
  199.     }
  200. }
  201.  
  202. function displayError(funcname, message) {
  203.  
  204.     try {
  205.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  206.                                       .getService(Components.interfaces.nsIPromptService);
  207.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
  208.                                .getService(Components.interfaces.nsIStringBundleService)
  209.                                .createBundle("chrome://autoconfig/locale/autoconfig.properties");
  210.  
  211.          var title = bundle.GetStringFromName("autoConfigTitle");
  212.          var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
  213.          promptService.alert(null, title, msg + " " + message);
  214.     }
  215.     catch(e) { }
  216. }
  217.  
  218. function getenv(name) {
  219.     try {
  220.         var environment = Components.classes["@mozilla.org/process/environment;1"].
  221.             getService(Components.interfaces.nsIEnvironment);
  222.         return environment.get(name);
  223.     }
  224.     catch(e) {
  225.         displayError("getEnvironment", e);
  226.     }
  227. }
  228.  
  229.